import type { RouteRecordRaw } from "vue-router";
import { createWebHistory, createRouter } from "vue-router";

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        name: "layout",
        redirect: "/square",
        component: () => import("@/layout/layout.vue"),
        children: [
            {
                path: "square",
                name: "square",
                component: () => import("@/views/square/index.vue"),
                meta: {
                    title: "社区广场",
                    icon: "square",
                },
            },
            {
                path: "profile",
                name: "profile",
                component: () => import("@/views/profile/index.vue"),
                meta: {
                    title: "个人中心",
                    icon: "profile",
                },
            },
        ],
    },
    // {
    //     path: "/",
    //     name: "layout",
    //     redirect: "layout",
    //     children: [
    //         {
    //             path: "/square/index",
    //             name: "squareIndex",
    //             component: () => import("@/views/square/index.vue"),
    //         },
    //         {
    //             path: "/moment/index",
    //             name: "momentIndex",
    //             component: () => import("@/views/moment/index.vue"),
    //         },
    //     ],
    // },
    {
        path: "/login",
        name: "login",
        redirect: "/login/index",
        children: [
            {
                path: "index",
                name: "loginIndex",
                component: () => import("@/views/login/index.vue"),
            },
            {
                path: "register",
                name: "loginRegister",
                component: () => import("@/views/login/register.vue"),
            },
        ],
    },
];

const router = createRouter({
    history: createWebHistory(),
    routes,
});

router.beforeEach((to, from, next) => {
    // next();
    if (
        to.fullPath !== "/login/index" &&
        to.fullPath !== "/login/register" &&
        !localStorage.getItem("token")
    ) {
        next({ path: "/login/index" });
    } else {
        next();
    }
});

export default router;
